/**
 * Copyright 2018 orivil.com. All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found at https://mit-license.org.
 */

package access

import (
	"net/rpc"
	"net"
	"net/http"
	"gitee.com/tugoer/orivil/log"
	"os"
)

type Access int

func (a *Access) GetToken(cfg *Config, reply *string) error {
	token := GetToken(cfg)
	if token.ErrCode == 0 {
		*reply = token.Value
		return nil
	} else {
		return token.Error
	}
}

func (a *Access) GetTicket(cfg *Config, ticket *string) error {
	t := GetTicket(cfg)
	if t.ErrCode == 0 {
		*ticket = t.Value
		return nil
	} else {
		return t.Error
	}
}

// 运行 rpc 服务器
func RunServer(address string) error {
	access := new(Access)
	rpc.Register(access)
	rpc.HandleHTTP()
	l, e := net.Listen("tcp", address)
	if e != nil {
		return e
	}
	log.Init.Printf("pid:[%d] listen on: [%s]\n", os.Getpid(), address)
	return http.Serve(l, nil)
}